package com.sanbi.common.util;


import lombok.extern.slf4j.Slf4j;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;

/**
 * @desc 日期处理工具
 * @author wanggang
 * @time 2024/8/30 15:49
 */
@Slf4j
public class DateTimeUtil {
    private static final DateTimeFormatter FORMAT = DateTimeFormatter.ofPattern("ss mm HH dd MM ? yyyy");

    public enum TimeCycle {
        YEAR, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND
    }

    /**
     * @desc 根据时区转换时间
     * @author wanggang
     * @time 2024/8/30 15:52
     * @param dateTime
     * @param originZoneId
     * @param targetZoneId
     */
    public static LocalDateTime convertDateTimeByTimezone(LocalDateTime dateTime, ZoneId originZoneId,
                                                     ZoneId targetZoneId)
    {
        return dateTime.atZone(originZoneId).withZoneSameInstant(targetZoneId).toLocalDateTime();
    }

    public static long getCurrentTimestamp(){
        return Instant.now().toEpochMilli();
    }

    public static LocalDateTime parseDateTime(String datetime) {
        LocalDateTime parseLocalDate = null;
        try {
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            parseLocalDate = LocalDateTime.parse(datetime, dtf);
        } catch (Exception e) {
            //格式有误时抛出
            log.error("时间格式错误", e);
        }

        return parseLocalDate;
    }
    /**
     * @desc 判断是否为当前日期
     * @author wanggang
     * @time 2022/7/14 16:35
     */
    public static boolean isCurrentDate(LocalDateTime dateTime) {
        if (dateTime == null){
            return false;
        }
        return LocalDate.now().isEqual(dateTime.toLocalDate());
    }

    /**
     * 将LocalDateTime转换为cron表达式的字符串。
     *
     * @param dateTime 要转换的LocalDateTime
     * @return cron表达式
     */
    public static String toCronExpression(LocalDateTime dateTime) {
        return dateTime.format(FORMAT);
    }

    /**
     * 将指定的 LocalDateTime 对象转换为 指定周期的 cron 表达式字符串
     * @param dateTime LocalDateTime 对象
     * @param timeCycle 时间周期枚举值
     * @return cron 表达式字符串
     */
    public static String toCronExpression(LocalDateTime dateTime, TimeCycle timeCycle) {
        String cron = null;
        switch (timeCycle) {
            case YEAR:
                cron = String.format("%d %d %d %d %d ? *", dateTime.getSecond(),
                        dateTime.getMinute(), dateTime.getHour(), dateTime.getDayOfMonth(),
                        dateTime.getMonthValue());
                break;
            case MONTH:
                cron = String.format("%d %d %d %d * ? *", dateTime.getSecond(),
                        dateTime.getMinute(), dateTime.getHour(), dateTime.getDayOfMonth());
                break;
            case WEEK:
                cron = String.format("%d %d %d ? * %d *", dateTime.getSecond(),
                        dateTime.getMinute(), dateTime.getHour(), dateTime.getDayOfWeek().getValue() % 7);
                break;
            case DAY:
                cron = String.format("%d %d %d * * ? *", dateTime.getSecond(),
                        dateTime.getMinute(), dateTime.getHour());
                break;
            case HOUR:
                cron = String.format("%d %d * * * ? *", dateTime.getSecond(),
                        dateTime.getMinute());
                break;
            case MINUTE:
                cron = String.format("%d * * * * ? *", dateTime.getSecond());

                break;
            case SECOND:
                cron = "0/1 * * * * ? *";
                break;
            default:
                throw new IllegalArgumentException("Unknown time cycle: " + timeCycle);
        }
        return cron;
    }

    public static void main(String[] args) {
        ZoneId mxZoneId = TimeZone.getTimeZone("GMT-6:00").toZoneId();
        ZoneId zaZoneId = TimeZone.getTimeZone("GMT+2:00").toZoneId();
        LocalDateTime now=LocalDateTime.now();
        LocalDateTime convertedTime=convertDateTimeByTimezone(now,mxZoneId,zaZoneId);
        String msg=String.format("mx时间：%s, za 时间：%s",now,convertedTime);
        System.out.println(getCurrentTimestamp());
    }

}
